﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Microsoft.Reporting.WebForms;

namespace Catering.Catering_Page.richang
{
    public partial class AteNumberYearOrMonth : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy'})");
                this.txt_EndTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                loadPage();
            }
        }

        private void loadPage()
        {
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                Bind_Data_to_Controls.Bind_Area_ddl(ddl_Area, isAdmin, groupid, companyid);
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class, isAdmin, classid);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {


            string datetype = this.Dr_DateType.SelectedItem.Value;
            string strstart = this.txt_StartTime.Text;
            string strend = this.txt_EndTime.Text;
            DateTime starttime = DateTime.Now;
            DateTime endtime = DateTime.Now;
            if (string.IsNullOrEmpty(strstart)) return;
            if (datetype.Equals("Other") && string.IsNullOrEmpty(strend)) return;
            if (datetype.Equals("Year"))
            {
                DateTime.TryParse(strstart + "-01-01 00:00:11", out starttime);
            }
            else
            {
                DateTime.TryParse(strstart, out starttime);
            }
            DateTime.TryParse(strend, out endtime);
            string classid = this.ddl_class.SelectedValue;
            string groupid = this.ddl_Area.SelectedValue;
            View_Monitor vm = new View_Monitor();
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();

            if (datetype.Equals("Other"))
            {
                string TITLE = Bind_Data_to_Controls.Bind_ReportTitle(groupid, classid, starttime, endtime, true, "人员统计",true);
                ReportDataSource rds = new ReportDataSource("DataSet1", vm.GetDynamicAte(pid, groupid, classid, starttime, endtime, TITLE));
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(rds);
                ReportViewer1.LocalReport.ReportPath = @"Catering_Page\richang\Report\Ate_Number_YearOrMonth01.rdlc";
                ReportViewer1.LocalReport.Refresh();
                ReportViewer1.Visible = true;
            }
            else
            {
                string TITLE = Bind_Data_to_Controls.Bind_ReportTitle(groupid, classid, starttime, "人员统计");
                ReportDataSource rds = new ReportDataSource("DataSet1", vm.GetDynamicAte(pid, groupid, classid, starttime, TITLE));
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.ReportPath = @"Catering_Page\richang\Report\Ate_Number_YearOrMonth.rdlc";
                ReportViewer1.LocalReport.DataSources.Add(rds);
                ReportViewer1.LocalReport.Refresh();
                ReportViewer1.Visible = true;
            }
        }

        protected void ddl_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_Area.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class);
            }
        }

        protected void Dr_DateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Dr_DateType.SelectedItem != null)
            {
                if (Dr_DateType.SelectedItem.Value.Equals("Year"))
                {
                    this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy'})");
                    this.txt_StartTime.Text = "";
                    this.othertype01.Visible = false;
                    this.othertype02.Visible = false;
                }
                else if (Dr_DateType.SelectedItem.Value.Equals("Month"))
                {
                    this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-M'})");
                    this.txt_StartTime.Text = "";
                    this.othertype01.Visible = false;
                    this.othertype02.Visible = false;
                }
                else
                {
                    this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                    this.txt_StartTime.Text = "";
                    this.othertype01.Visible = true;
                    this.othertype02.Visible = true;
                }
            }
        }
    }
}